package com.helloandroid;

import java.io.FileNotFoundException;
import java.io.IOException;

import com.helloandroid.gallerysample.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class GallerysampleActivity extends Activity {
	private static Bitmap Image = null;
	private static Bitmap rotateImage = null;
	private ImageView imageView;
	private static final int GALLERY = 1;

	public String getDeviceID() {
		TelephonyManager tManager = (TelephonyManager) this
				.getSystemService(Context.TELEPHONY_SERVICE);
		String uid = tManager.getDeviceId();
		if(uid.length()<15){
			for(int i=0;i<15-uid.length();i++){
				uid = "0"+uid;
			}
		}
		
		return uid;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		Log.d("test",""+getDeviceID());
		imageView = (ImageView) findViewById(R.id.imageView1);

		Button gallery = (Button) findViewById(R.id.button1);
		gallery.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				imageView.setImageBitmap(null);
				if (Image != null)
					Image.recycle();
				Intent intent = new Intent();
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY);
			}
		});
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == GALLERY && resultCode != 0) {
			Uri mImageUri = data.getData();				
			try {
				
				Image = Media.getBitmap(this.getContentResolver(), mImageUri);
				if (getOrientation(getApplicationContext(), mImageUri) != 0) {
					Matrix matrix = new Matrix();
					matrix.postRotate(getOrientation(getApplicationContext(), mImageUri));
					if (rotateImage != null)
						rotateImage.recycle();
					rotateImage = Bitmap.createBitmap(Image, 0, 0, Image.getWidth(), Image.getHeight(), matrix,
							true);
					imageView.setImageBitmap(rotateImage);
				} else
					imageView.setImageBitmap(Image);				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static int getOrientation(Context context, Uri photoUri) {
		/* it's on the external media. */
		Cursor cursor = context.getContentResolver().query(photoUri,
				new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

		if (cursor.getCount() != 1) {
			return -1;
		}

		cursor.moveToFirst();
		return cursor.getInt(0);
	}
}